package com.xhsj.user.sms.announcement.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xhsj.user.sms.announcement.entity.TbEnterpriseAnnouncement;
import com.xhsj.user.sms.announcement.service.TbEnterpriseAnnouncementService;
import com.xhsj.user.utils.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//import org.springframework.security.access.prepost.PreAuthorize;


/**
 * <p>
 * 企业公告 前端控制器
 * </p>
 *
 * @author suxiaolin
 * @since 2020-06-23
 */
@Slf4j
@Api(tags = "企业公告")
@RestController
@RequestMapping("/announcement/tbEnterpriseAnnouncement")
public class TbEnterpriseAnnouncementController {

	@Autowired
    public TbEnterpriseAnnouncementService service;

	@ApiOperation(value = "企业公告,保存/修改", notes = "企业公告,保存和修改方法")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
    //@PreAuthorize("hasAuthority('tb:enterprise:announcement:edit')")
	public Message save(@RequestBody TbEnterpriseAnnouncement data){
    	try {
    		service.save(data);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
        	return Message.error(1,"失败");
		}

	}


	@ApiOperation(value = "企业公告和附件,保存/修改", notes = "企业公告和附件,保存和修改方法")
	@RequestMapping(value = "/saveAndFile", method = RequestMethod.POST)
	public Message saveAndFile(@RequestBody TbEnterpriseAnnouncement data){
		try {
			service.saveAndFile(data);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
			return Message.error(1,"失败");
		}

	}


	@ApiOperation(value = "企业公告删除", notes = "通过id删除企业公告信息。")
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
    //@PreAuthorize("hasAuthority('tb:enterprise:announcement:delete')")
	public Message delete(@RequestParam(value="id") String id) {
		try {
			service.removeById(id);
        	return Message.success("成功");
		} catch (Exception e) {
			log.error("删除失败:{}",e);
            return Message.error(1,"失败");
		}

	}

	@ApiOperation(value = "批量删除", notes = "批量删除。")
	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	public Message batchDelete(@RequestBody List<String> list) {
		try {
			service.removeByIds(list);
    		return Message.success("成功");
		} catch (Exception e) {
    		log.error("删除失败:{}",e);
    		return Message.error(1,"失败");
		}
    }

	@ApiOperation(value = "分页查询企业公告list", notes = "带参数查询,{\"page\": 1,\"size\": 5,......}。支持模糊查询")
	@RequestMapping(value="/findList",method = RequestMethod.POST)
    //@PreAuthorize("hasAuthority('tb:enterprise:announcement:view')")
	public Message findList(@RequestBody Map<String,Object> data) {
        try {
        	IPage<TbEnterpriseAnnouncement> selectPage = service.selectPage(data);
    		return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
        } catch (Exception e) {
			log.error("查询异常:{}",e);
    		return Message.error(1,"失败");
        }
	}

	@ApiOperation(value = "查询企业公告所有数据list", notes = "查询企业公告所有数据list。")
	@RequestMapping(value="/findAll",method = RequestMethod.POST)
    //@PreAuthorize("hasAuthority('tb:enterprise:announcement:view')")
	public Message findAll(){
        return Message.success("成功",service.selectAll());
	}


	@ApiOperation(value = "通过id获取企业公告实体类", notes = "通过id获取企业公告实体类。")
	@RequestMapping(value="/getById",method = RequestMethod.GET)
    //@PreAuthorize("hasAuthority('tb:enterprise:announcement:view')")
	public Message getById(@RequestParam(value="id") String id) {
    	return Message.success("成功",service.getById(id));
	}

	@ApiOperation(value = "通过id获取实体类详细信息", notes = "通过id获取实体类详细信息。")
	@RequestMapping(value="/detail",method = RequestMethod.GET)
    //@PreAuthorize("hasAuthority('tb:enterprise:announcement:view')")
	public Message detail(@RequestParam(value="id") String id) {
    	return Message.success("成功",service.getById(id));
	}


	@ApiOperation(value = "分页查询企业公告list包含通过当前用户查看是否接收", notes = "带参数查询,{\"page\": 1,\"size\": 5,......}。支持模糊查询")
	@RequestMapping(value="/findListByUserId",method = RequestMethod.POST)
	public Message findListByUserId(@RequestBody Map<String,Object> data) {
		try {
			IPage<Map<String,Object>> selectPage = service.findListByUserIdPage(data);
			return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
		} catch (Exception e) {
			log.error("查询异常:{}",e);
			return Message.error(1,"失败");
		}
	}


	@ApiOperation(value = "查看详情接口根据id", notes = "查看详情接口根据id")
	@RequestMapping(value="/findListById",method = RequestMethod.GET)
	public Message findListById(@RequestParam(value="id") String id) {
		try {
			List<TbEnterpriseAnnouncement> findListById = service.findListById(id);
			return Message.success("成功",findListById);
		} catch (Exception e) {
			log.error("查询异常:{}",e);
			return Message.error(1,"失败");
		}
	}

	@ApiOperation(value = "通过用户id查看公告信息", notes = "通过用户id查看公告信息")
	@RequestMapping(value="/getListByUserId",method = RequestMethod.POST)
	public Message getListByUserId(@RequestBody Map<String,Object> data) {
		try {
			IPage<TbEnterpriseAnnouncement> selectPage = service.getListByUserId(data);
			return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
		} catch (Exception e) {
			log.error("查询异常:{}",e);
			return Message.error(1,"失败");
		}
	}


	@ApiOperation(value = "企业公告删除", notes = "通过id删除企业公告信息。")
	@RequestMapping(value = "/deleteById", method = RequestMethod.GET)
	public Message deleteById(@RequestParam(value="id") String id) {
		try {
			service.deleteById(id);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("删除失败:{}",e);
			return Message.error(1,"失败");
		}

	}


	@ApiOperation(value = "企业公告编辑", notes = "企业公告编辑。")
	@RequestMapping(value = "/updateAndFile", method = RequestMethod.POST)
	public Message updateAndFile(@RequestBody TbEnterpriseAnnouncement data) {
		try {
			service.updateAndFile(data);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("删除失败:{}",e);
			return Message.error(1,"失败");
		}

	}


//	@ApiOperation(value = "公告用户确定", notes = "公告用户确定")
//	@RequestMapping(value="/findListById",method = RequestMethod.GET)
//	public Message findListById(@RequestParam(value="id") String id) {
//		try {
//			List<Map<String,Object>> findListById = service.findListById(id);
//			return Message.success("成功",findListById);
//		} catch (Exception e) {
//			log.error("查询异常:{}",e);
//			return Message.error(1,"失败");
//		}
//	}

}

